local Buff = MClass("Buff")
local BUFF_ZENGYI = 1 --蓝色
local BUFF_JIANYI = 2 --红色
local COLOR_ZENGYI = cc.c4b(133,217,227,255)
local COLOR_JIANYI = cc.c4b(252,112,76,255)
local TAG_CD1 = 101
local TAG_CD2 = 102
--ctor 初始化UI init设置静态数据 setData设置动态数据
function Buff:ctor()
    --self.root 默认为根节点
    --BG
    self.mCdNode = self:findChild("Panel")
    self.bg = self:findChild("Panel/bg")
    --ICON
    self.iconNode = self:findChild("Panel/icon")
    local zorder = self.iconNode:getLocalZOrder()
    self.mask = self:findChild("Panel/mask")
    self.label = self:findChild("Panel/num")
    --self.clipNode = self:CreateClippingNode(self.mask,self.icon)
    --self.mCdNode:addChild(self.clipNode,zorder)
    --CD 
    self.mCdMask1 = self.mCdNode:findChild("battle_buf_cd_1")
    assert(self.mCdMask1)
    self.mCdMask1:setVisible(false)

    self.cdSprite1 = self.mCdNode:getChildByTag(TAG_CD1)
    if not self.cdSprite1 then
        self.cdSprite1 = cc.ProgressTimer:create( self.mCdMask1 )
        self.cdSprite1:setType( cc.PROGRESS_TIMER_TYPE_RADIAL )
        self.cdSprite1:setScaleX(-1)
        self.cdSprite1:setMidpoint(display.ANCHOR_POINTS[1])
        self.cdSprite1:setPosition(self.mCdMask1:getPosition())
        self.mCdNode:addChild(self.cdSprite1)
        self.cdSprite1:setTag(TAG_CD1)
    end

    self.mCdMask2 = self.mCdNode:findChild("battle_buf_cd_2")
    assert(self.mCdMask2)
    self.mCdMask2:setVisible(false)

    self.cdSprite2 = self.mCdNode:getChildByTag(TAG_CD2)
    if not self.cdSprite2 then

        self.cdSprite2 = cc.ProgressTimer:create( self.mCdMask2 )
        self.cdSprite2:setType( cc.PROGRESS_TIMER_TYPE_RADIAL )
        self.cdSprite2:setScaleX(-1)
        self.cdSprite2:setMidpoint(display.ANCHOR_POINTS[1])
        self.cdSprite2:setPosition(self.mCdMask2:getPosition())
        self.mCdNode:addChild(self.cdSprite2)
        self.cdSprite2:setTag(TAG_CD2)
    end
    self.label:setLocalZOrder(self.cdSprite2:getLocalZOrder()+1) 

    --add onEnter onExit
    -- local function onNodeEvent(event)
    --     if "enter" == event then
    --         self:onEnter()
    --     elseif "exit" == event then
    --         self:onExit()
    --     end
    -- end
    -- self:registerScriptHandler(onNodeEvent) 

	self:init()
end

-- function Buff:onEnter()
-- end

-- function Buff:onExit()
--     self:unregisterScriptHandler()
-- end

function Buff:CreateClippingNode(stencilNode,contentNode)
    if tolua.isnull(stencilNode) or tolua.isnull(contentNode) then
        print("Buff getOrCreateHeroIconClipping fail")
    else
        print("Buff getOrCreateHeroIconClipping ok")
        contentNode:retain()
        if contentNode:getParent() then
            contentNode:removeFromParent()
        end

        stencilNode:retain()
        if stencilNode:getParent() then
            stencilNode:removeFromParent()
        end
        local clipNode = cc.ClippingNode:create()
        clipNode:setInverted(false)
        clipNode:setAlphaThreshold(0.5)
        if stencilNode then
            clipNode:setStencil(stencilNode)
            stencilNode:setVisible(true)
            contentNode:setContentSize(stencilNode:getContentSize())
            contentNode:setPosition(stencilNode:getPosition())
        end
        clipNode:addChild(contentNode)
        contentNode:release()
        stencilNode:release()
        return clipNode
    end
end

function Buff:init()
    BattleUIEvent:addObjToFrameUpdate(self,Buff.updateFrame)
   self.lastBuffId = nil
   self.targetId = nil
   self.cdSprite1:setVisible(true)
   self.cdSprite2:setVisible(false)
   self:initTouch()
end

function Buff:reset()
	self.cdSprite1:setVisible(false)
	self.cdSprite2:setVisible(false)
end

 function Buff:setTargetId(id)
 	self.targetId = id
 end

function Buff:setType(displaySort)
    if   displaySort == BUFF_JIANYI then
        self.cdSprite1:setVisible(false)
        self.cdSprite2:setVisible(true)
        self.cdSprite = self.cdSprite2
        self.label:getVirtualRenderer():setColor(COLOR_JIANYI)
    elseif displaySort == BUFF_ZENGYI then
        self.cdSprite1:setVisible(true)
        self.cdSprite2:setVisible(false)
        self.cdSprite = self.cdSprite1
        self.label:getVirtualRenderer():setColor(COLOR_ZENGYI)
    end
end

function Buff:updateFrame()
    --长按tips
    if not self.startTime then
        return
    end
    self.duleTime = app.session:getServerTime() or app:getUTCTime()
    if self.duleTime-self.startTime > 0 and not self.bShowTips then
        self.bShowTips = true
        local param = {tipsType = Enums.TipsType.classSkill,dataTemp = self,}
        app:dispatchCustomEvent("EVENT_OPEN_FLOAT", {params=param})
    end
end

function Buff:initTouch()
    local function touchEvent(sender, eventType)
        if eventType == ccui.TouchEventType.ended or eventType == ccui.TouchEventType.canceled then
            self.startTime = nil
            self.bShowTips = false
            app:dispatchCustomEvent("EVENT_CLOSE_FLOAT")
        elseif eventType == ccui.TouchEventType.began then
            self.startTime = app.session:getServerTime() or app:getUTCTime()
        end
    end
    self.mCdNode:addTouchEventListener(touchEvent)
end

function Buff:getName()
    return self.name
end

function Buff:getIcon()
    return self.icon
end

function Buff:getDes()
    return L(self.desc) or "Test"
end

function Buff:setData(data)
	self:setVisible(true)
    self:setType(data.displaySort)
	self.buffId = data.buffId
	if self.buffId ~= self.lastBuffId then
		self.lastBuffId = self.buffId
		self.maxTime = data.maxTime
	else
		if data.leftTime > self.maxTime then
			self.maxTime = data.leftTime
		end
	end
	local percent = data.leftTime/self.maxTime
    self.cdSprite:setPercentage(100*percent)
	self.iconNode:loadTexture(data.icon)
    self.icon = data.icon
    self.name = data.name
    self.desc = data.desc
    self.label:setString(data.layerCount)
    if data.layerCount > 1 then
        self.label:setVisible(true)
    else
        self.label:setVisible(false)
    end
    
    self.cdSprite:setVisible(data.bShowCD)
    --dump(data.bShowCD)
end

return Buff